<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
    <link rel="stylesheet" th:href="@{/semantic/semantic.min.css}"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css">
</head>
<div style="margin:50px 50px 50px 50px">
    <form class="layui-form layui-form-pane" lay-filter="projectInfo">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui blue book icon"></i>项目名称</label>
                <div class="layui-input-block">
                    <input type="text" name="projectName" required lay-verify="required" autocomplete="off"
                           placeholder="请输入项目名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui blue calendar alternate outline icon"></i>建立时间</label>
                <div class="layui-input-block">
                    <input type="text" name="creatDate" id="creatDate" lay-verify="date" placeholder="yyyy-MM-dd"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui blue calendar alternate outline icon"></i>结束时间</label>
                <div class="layui-input-block">
                    <input type="text" name="endDate" id="endDate" lay-verify="date" placeholder="yyyy-MM-dd"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>客户ID</label>
                <div class="layui-input-block">
                    <input type="text" name="clientId" id="clientId" required lay-verify="exits" autocomplete="off"
                           placeholder="请输入客户ID" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>文件地址</label>
                <div class="layui-input-block">
                    <input type="text" name="projectAddress" required lay-verify="required" autocomplete="off"
                           placeholder="请输入文件地址" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui blue font icon"></i>项目类型</label>
                <div class="layui-input-inline">
                    <select name="projectType" lay-verify="required">
                        <option value="">请选择类型</option>
                        <option value="1">一级</option>
                        <option value="2">二级</option>
                        <option value="3">三级</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>模型单价</label>
                <div class="layui-input-block">
                    <input type="number" name="modelPrice" required lay-verify="required" autocomplete="off"
                           placeholder="请输入模型单价" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>渲染单价</label>
                <div class="layui-input-block">
                    <input type="number" name="renderPrice" required lay-verify="required" autocomplete="off"
                           placeholder="请输入模型单价" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><i class="ui violet user outline icon"></i>后期单价</label>
                <div class="layui-input-block">
                    <input type="number" name="lastPrice" required lay-verify="required" autocomplete="off"
                           placeholder="请输入后期单价" class="layui-input">
                </div>
            </div>
            <input type="hidden" name="projectId" class="layui-input">
        </div>

        <div class="layui-form-item" style="margin-left: -110px">

            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="update">更新</button>
                <button type="reset" class="layui-btn layui-btn-danger">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" th:src="@{/js/jquery-3.4.1.min.js}"></script>
<script type="text/javascript" th:src="@{/semantic/semantic.min.js}"></script>
<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
<script type="text/javascript" th:src="@{/js/upload.js}"></script>
<script th:inline="javascript">
    function isExist() {
        let flag = false;
        $.ajax({
            url: "/client/select",
            method: "post",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"clientId": Number($("#clientId").val())}),
            dataType: "json",
            async: false,
            success: function (res) {
                if (res.result.length !== 0) {
                    flag = true;
                }
            }
        })

        return flag;
    }

    layui.use(['form', 'layedit', 'laydate'], function () {
        const form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#creatDate'
        });
        laydate.render({
            elem: '#endDate'
        });
        form.render('select');
        //监听提交
        form.verify({
            exits: function () {
                if (!isExist()) {
                    return "该客户不存在"
                }
            }
        });
        const project = [[${project}]];//得到model中的client
        form.val('projectInfo', {
            "projectId": project.projectId
            , "projectName": project.projectName // "name": "value"
            , "creatDate": project.creatDate
            , "endDate": project.endDate
            , "clientId": project.clientId
            , "projectAddress": project.projectAddress
            , "projectType": project.projectType
            , "modelPrice": project.modelPrice
            , "renderPrice": project.renderPrice
            , "lastPrice": project.lastPrice
        });
        form.on('submit(update)', function (data) {
            $.ajax({
                url: "/project/update",
                method: "post",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(data.field),
                dataType: "json",
                success: function () {
                    layer.alert('修改成功', {
                        skin: 'layui-layer-lan' //样式类名
                        , closeBtn: 0
                    });
                }
            })
            return false;
        });
    });
</script>